<template>
  <!--模态窗 多行-->
  <transition
    name="custom-classes-transition"
    enter-active-class="animated fadeInDown"
    leave-active-class="animated fadeOutUp"
  >
    <div class="modal-ctrl-body" v-show="roleSetFunctionObj.isShow">
      <div class="modal-ctrl-main even-row-ctrl" >

        <div class="modal-title">
          <p>权限因子授权</p>
        </div>
        <div class="modal-ctrl-content" style="height: 300px;overflow: auto">

          <div class="authority-factor" :ref="'functionModal'" id="treeGidRole">
            <!-- @cell-click = cellClick -->
            <zk-table
              ref="table"
              sum-text="sum"
              index-text="#"
              :data="data"
              :columns="columns"
              :stripe="props.stripe"
              :border="props.border"
              :show-header="props.showHeader"
              :show-summary="props.showSummary"
              :show-row-hover="props.showRowHover"
              :show-index="props.showIndex"
              :tree-type="props.treeType"
              :is-fold="props.isFold"
              :expand-type="props.expandType"
              :selection-type="props.selectionType" >
              <!-- <template slot="$expand" scope="scope">
                {{ `My name is ${scope.row.name},
                   this rowIndex is ${scope.rowIndex}.`
                 }}
              </template> -->
              <template slot="checkbox" slot-scope="scope">
               <!--  {{ scope.row.id }}-->
               <!-- <input type="checkbox" @change="changeCheckbox(scope)" />-->
                <input type="checkbox"
                       :id="scope.row.id" class="tpye-menu"
                       :data-pId="scope.row.pId ? scope.row.pId : '-1' "
                       :value="scope.row.id"
                       @click="selectMenu"
                >
                <!--<div class=""></div>-->
              </template>
              <template slot="factor" slot-scope="scope">
                   <!--{{ scope.row.actions}}-->
                <!-- :disabled="isDisabled(scope)" -->
                <label v-for="item in scope.row.actions">
                  <input type="checkbox" disabled="disabled" :title="item.name" :id="item.id" :value="item.id" class="type-qxyz"/>
                  <span>{{ item.name }}</span>
                </label>

              </template>
            </zk-table>
          </div>

        </div>
        <div class="btns text-r">
          <button type="button" class="btn btn-default" @click="close">取消</button>
          <button type="button" class="btn btn-info" @click="subBtn">确定</button>
        </div>
        <!--{{userDetailsModal.obj}}-->
      </div>
    </div>
  </transition>
</template>

<script>
    export default {
      name: "RoleFunctionModal",
      props:["roleSetFunctionObj","roleId"],
      data() {
        return {
          props: {
            stripe: false,
            border: false,
            showHeader: true,
            showSummary: false,
            showRowHover: true,
            showIndex: true,
            treeType: true,
            isFold: true,
            expandType: false,
            selectionType: false,
          },
          data: [
            {
              name: 'Jack',
              sex: 'male',
              likes: ['football', 'basketball'],
              score: 10,
              id:"wqew",
              children: [
                {
                  name: 'Ashley',
                  sex: 'female',
                  likes: ['football', 'basketball'],
                  score: 20,
                  children: [
                    {
                      name: 'Ashley',
                      sex: 'female',
                      likes: ['football', 'basketball'],
                      score: 20,
                    },
                    {
                      name: 'Taki',
                      sex: 'male',
                      likes: ['football', 'basketball'],
                      score: 10,
                      children: [
                        {
                          name: 'Ashley',
                          sex: 'female',
                          likes: ['football', 'basketball'],
                          score: 20,
                        },
                        {
                          name: 'Taki',
                          sex: 'male',
                          likes: ['football', 'basketball'],
                          score: 10,
                          children: [
                            {
                              name: 'Ashley',
                              sex: 'female',
                              likes: ['football', 'basketball'],
                              score: 20,
                            },
                            {
                              name: 'Taki',
                              sex: 'male',
                              likes: ['football', 'basketball'],
                              score: 10,
                            },
                          ],
                        },
                      ],
                    },
                  ],
                },
                {
                  name: 'Taki',
                  sex: 'male',
                  likes: ['football', 'basketball'],
                  score: 10,
                },
              ],
            }
          ],
          columns: [
            {
              label: '菜单名称',
              prop: 'displayName',
              width: '205px',
            },
            {
              label: '菜单授权',
              prop: 'checkbox',
              minWidth: '100px',
              type: 'template',
              template: 'checkbox',
            },
            {
              label: '权限因子授权',
              prop: 'arr',
              minWidth: '360px',
              type: 'template',
              template: 'factor',
            },
          ],
          rowIndexAry: [],
          RoleFunctionId:null
        }
      },
      watch:{
        roleSetFunctionObj:{
          handler(v1){
            if( v1.isShow ){
              //console.log(this.roleId);
              this.RoleFunctionId = this.roleId;
              //渲染菜单和权限因子列
              this.getAllRoleFunction();
              $("#" + $(this.$refs.functionModal)[0].id + "").find("input:checkbox").prop("checked",false);
            }
          },
          deep:true
        }
      },
      methods:{
        changeCheckbox(scope){
          // this.rowIndexAry.push(scope.rowIndex)
          // //console.log(this.rowIndexAry)
          //console.log(scope,1)
        },
        changeFactor(scope){
          //console.log(scope,2)
        },
        isDisabled(scope){
          // //console.log(this.rowIndexAry)
          //console.log(scope)
        },
        subBtn(){
          let menuListArr = $(this.$refs.functionModal).eq(0).find("input.tpye-menu:checked");
          let qxyzListArr = $(this.$refs.functionModal).eq(0).find("input.type-qxyz:checked");
          let paramsMenu = [];
          let paramsQXYZ = [];
          ////console.log(qxyzListArr);
          //console.log(menuListArr);

          //遍历得到菜单id
          for( let i=0;i<menuListArr.length;i++ ){
            paramsMenu.push(menuListArr[i].value)
          }
          //遍历得到因子id
          for( let i=0;i<qxyzListArr.length;i++ ){
            paramsQXYZ.push(qxyzListArr[i].value)
          }
          let params = {
            functionIds:paramsMenu,
            actionIds:paramsQXYZ
          };

          //console.log(params);
          //return false;
          this.$axios.post(this.HOST+"/roles/giveRolePrivilege/"+this.RoleFunctionId,params)
            .then((data)=>{
              if( data.data.success ){
                this.s_alert("授权成功！");
                // alert("授权成功！");
                this.s_logwrite("基础信息管理-角色管理","进行角色功能授权");
                this.close();
              }
              //console.log(data);
            })
            .catch((err)=>{
              //console.log(err);
            });
          //console.log(params);
        },
        //获取所有角色菜单和权限因子列
        getAllRoleFunction(){

          this.$axios.get(this.HOST+"/priFuncs/scheme/page",{
            params:{
              pageNum:1,
              pageSize:-1
            }
          })
            .then((data)=>{
              this.data = data.data.data;
              //console.log(data);
              //查询角色拥有的权限
              this.getRoleFunction();
            })
            .catch((err)=>{
              //console.log(err);
            })
        },
        //查询角色拥有的权限因子
        getRoleFunction(){
          this.$axios.get(this.HOST+"/priFuncs/menu/page/byRole/"+this.RoleFunctionId)
            .then((data)=>{
              let allFunction = data.data;
              //console.log(allFunction);
              for( let i=0;i<allFunction.length;i++ ){
                //查找权限因子
                this.findFunction(allFunction[i]);

                let menu = $("#"+ allFunction[i].id +"");
                let qxyzTd = menu.parents("tr").find("td").eq(3);
                qxyzTd.find("input:checkbox").prop("disabled",false);

              }
              //console.log(data);
            })
            .catch((err)=>{
              //console.log(err);
            })
        },
        //查找权限因子
        findFunction(item){
          if( item.actions.length > 0 ){
            for( let i=0;i<item.actions.length;i++ ){
              let menu = $("#"+ item.actions[i].id +"");
              //console.log(menu);
              menu.prop("checked",true);
            }
          }
          if( item.children ){
            this.findFunction(item.children)
          }else{
            $("#"+ item.id +"").prop("checked",true);
          }
        },
        //向上查找并且选中父级菜单
        findParentMenu(itemMenu){
          //console.log(itemMenu.dataset);

          if(itemMenu.dataset.pid){
            $("#" + itemMenu.dataset.pid + "").prop("checked",true);
            ////console.log($("#" + itemMenu.dataset.pid + ""));
          }else{
            return false;
          }

        },
        //关闭弹窗
        close(){
          this.$emit("roleFunctionModalClose")
        },
        //选择菜单
        selectMenu(ele){
          //console.log(ele.target.id);
          //console.log(ele.target.checked);
          let menu = $("#"+ ele.target.id +"");
          let qxyzTd = menu.parents("tr").find("td").eq(3);
          if( ele.target.checked ){
            qxyzTd.find("input:checkbox").prop("disabled",false);
          }else{
            qxyzTd.find("input:checkbox").prop("disabled",true);
            qxyzTd.find("input:checkbox").prop("checked",false);
          }
        }
      }
    }
</script>

<style scoped>

  .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
    height: 34px;
    line-height: 34px;
    padding: 0 8px;
  }

  .zk-table__cell-inner label{
    float: left;
    position: relative;
  }

  .zk-table__cell-inner label input{
    position: absolute;
    left: 0;
    top: -3px;
    width: 100%;
    height: 100%;
    z-index: 3;
    opacity: 0;
    cursor: pointer;
  }

  .zk-table__cell-inner label span{
    display: inline-block;
    padding: 0px 5px;
    margin-right: 5px;
    border-radius: 4px;
  }
  .zk-table__cell-inner label input:disabled{
    cursor: no-drop;
  }
  .zk-table__cell-inner label input:disabled + span{
    background-color: #fff;
    border:1px solid #ededed;
    color: rgba(51,51,51,0.5);
  }

  .zk-table__cell-inner label input +  span{
    color: #31b0d5;
    border:1px solid #31b0d5;
  }
  .zk-table__cell-inner label input:checked{
    opacity: 0;
  }
  .zk-table__cell-inner label input:checked +  span{
    color: #fff;
    background-color: #31b0d5;
    border:1px solid #31b0d5;
  }
  .zk-table__cell-inner label span{
    display: inline-block;
    margin-right: 10px;
    border-radius: 4px;
    width: 60px;
    height: 28px;
    padding: 0 5px;
    overflow: hidden;
    box-sizing: border-box;
    text-overflow:ellipsis;
    white-space: nowrap;
  }
  .fadeInDown,.fadeOutUp{
    animation-duration: .500s;
  }

</style>
